package com.miyake.dsp.osa.lab.wrapper;

import com.miyake.dsp.osa.lab.Calculator;
import com.miyake.dsp.osa.lab.EdgeNotFoundException;
import com.miyake.dsp.osa.lab.LabUiAnnotation;
import com.miyake.dsp.osa.lab.NoiseDetector;
@LabUiAnnotation(caption="Auto Noise Detector")
public class NoiseDetectorWrapper extends Calculator {
	public static final String IN_X = "In x";
	public static final String IN_Y = "In y";
	public static final String OUT_NOISELEVEL = "Noise Level";
	public static final String OUT_NOISESTART = "Noise Start";
	public static final String OUT_SIGNALSTART = "Signal Start";
	public static final String OUT_X = "Noise Xs";
	public static final String OUT_Y = "Noise Ys";
	public static final String OUT_Y_SORTED = "Sorted Y";
	public static final String OUT_X_SORTED = "Sorted X";
	public static final String OUT_DIFF = "Diff";
	public static final String OUT_THRESHOLD = "Threshold";
	public NoiseDetectorWrapper() {
		initInput(IN_X);
		initInput(IN_Y);
		initOutput(OUT_NOISELEVEL);
		initOutput(OUT_NOISESTART);
		initOutput(OUT_SIGNALSTART);
		initOutput(OUT_X);
		initOutput(OUT_Y);
		initOutput(OUT_Y_SORTED);
		initOutput(OUT_X_SORTED);
		initOutput(OUT_DIFF);
		initOutput(OUT_THRESHOLD);
	}
	
	@Override
	public synchronized void process() {
		double[] x = null;
		if (this.getInput(IN_X) != null) {
			x = this.getInput(IN_X).getRealPart();
		}
		
		double[] y = null;
		if (this.getInput(IN_Y) != null) {
			y = this.getInput(IN_Y).getRealPart();
		}
		NoiseDetector noiseDetector = null;
		try {
			noiseDetector = new NoiseDetector();
			noiseDetector.setData(x, y);
			setOutput(OUT_X, noiseDetector.getXout());
			setOutput(OUT_NOISELEVEL, noiseDetector.getNoiseFloor());
			setOutput(OUT_NOISESTART, noiseDetector.getNoiseStart());
			setOutput(OUT_SIGNALSTART, noiseDetector.getSignalStart());
			setOutput(OUT_Y, noiseDetector.getYout());
			setOutput(OUT_DIFF, noiseDetector.getDiffTrace());
			setOutput(OUT_THRESHOLD, noiseDetector.getThreshold());
		} catch (EdgeNotFoundException e) {
			setOutput(OUT_X, 0);
			setOutput(OUT_NOISELEVEL, 0);
			setOutput(OUT_NOISESTART, 0);
		}
		setOutput(OUT_Y_SORTED, noiseDetector.getSortedY());
		setOutput(OUT_X_SORTED, noiseDetector.getSortedX());

	}

	@Override
	public Object getSetting() {
		return null;
	}

}
